#include "staticfunction.h"
#include <QSettings>
#include <QCoreApplication>
#include <QFileInfo>
#include <QXmlStreamWriter>
#include <QXmlStreamReader>
#include <QDebug>

StaticFunction::StaticFunction(QObject *parent) : QObject(parent)
{

}

uint8_t StaticFunction::Get1BytesFromBuf(uint8_t *pBuf, int index)
{
    return pBuf[index];
}


uint16_t StaticFunction::Get2BytesFromBuf(uint8_t *pBuf, int index)
{
    uint16_t res = pBuf[index];
    res += (0x100 * pBuf[index+1]);
    return res;
}

uint32_t StaticFunction::Get4BytesFromBuf(uint8_t *pBuf, int index)
{
    uint32_t res = pBuf[index];
    res += (0x100 * pBuf[index + 1]);
    res += (0x10000 * pBuf[index + 2]);
    res += (0x1000000 * pBuf[index + 3]);
    return res;
}

int32_t StaticFunction::Getint32FromBuf(uint8_t *pBuf, int index)
{
    int32_t res = pBuf[index];
    res += (0x100 * pBuf[index + 1]);
    res += (0x10000 * pBuf[index + 2]);
    res += (0x1000000 * pBuf[index + 3]);
    return res;
}

int64_t StaticFunction::GetNBytesFromBuf(uint8_t *pBuf, int index, int length)
{
    int64_t res = 0;
    for (int i=0; i<length; i++)
    {
        res += pBuf[index + i] * (1 << (8 * i));
    }
    return res;
}

PACKAGE_INFO StaticFunction::GetPackageInfo()
{
	PACKAGE_INFO def = GetPackageInfoFromFile();
	QString appName = QCoreApplication::applicationName();
	QSettings app1(appName, "Config");
	QString m_type = app1.value("Package").toString();

	QString str = QString("%1\\Config").arg(appName);
	QSettings app(QSettings::NativeFormat, QSettings::UserScope, str, m_type);
	if (app.contains("lightBall1"))
	{
		def.lightConfigBall.brightness1 = app.value("lightBall1").toInt();
	}
	if (app.contains("lightBall2"))
	{
		def.lightConfigBall.brightness2 = app.value("lightBall2").toInt();
	}
	if (app.contains("lightBall3"))
	{
		def.lightConfigBall.brightness3 = app.value("lightBall3").toInt();
	}
	if (app.contains("lightBall4"))
	{
		def.lightConfigBall.brightness4 = app.value("lightBall4").toInt();
	}

	if (app.contains("lightCM1"))
	{
		def.lightConfigCM.brightness1 = app.value("lightCM1").toInt();
	}
	if (app.contains("lightCM2"))
	{
		def.lightConfigCM.brightness2 = app.value("lightCM2").toInt();
	}
	if (app.contains("lightCM3"))
	{
		def.lightConfigCM.brightness3 = app.value("lightCM3").toInt();
	}
	if (app.contains("lightCM3"))
	{
		def.lightConfigCM.brightness4 = app.value("lightCM4").toInt();
	}

	if (app.contains("lightCap1"))
	{
		def.lightConfigCap.brightness1 = app.value("lightCap1").toInt();
	}
	if (app.contains("lightCap2"))
	{
		def.lightConfigCap.brightness2 = app.value("lightCap2").toInt();
	}
	if (app.contains("lightCap3"))
	{
		def.lightConfigCap.brightness3 = app.value("lightCap3").toInt();
	}
	if (app.contains("lightCap4"))
	{
		def.lightConfigCap.brightness4 = app.value("lightCap4").toInt();
	}

	if (app.contains("lightSubstrate1"))
	{
		def.lightConfigSubstrate.brightness1 = app.value("lightSubstrate1").toInt();
	}
	if (app.contains("lightSubstrate2"))
	{
		def.lightConfigSubstrate.brightness2 = app.value("lightSubstrate2").toInt();
	}
	if (app.contains("lightSubstrate3"))
	{
		def.lightConfigSubstrate.brightness3 = app.value("lightSubstrate3").toInt();
	}
	if (app.contains("lightSubstrate4"))
	{
		def.lightConfigSubstrate.brightness4 = app.value("lightSubstrate4").toInt();
	}

	return def;
}

PACKAGE_INFO StaticFunction::GetPackageInfoFromFile()
{
	QString appName = QCoreApplication::applicationName();
	QSettings app1(appName, "Config");
	QString chipType = app1.value("ChipType").toString();


	QString m_type = app1.value("Package").toString();
	PACKAGE_INFO def;
	QString path = QString("%1/Config/package/%3/%2.cfg").arg(QCoreApplication::applicationDirPath()).arg(m_type).arg(chipType);
	QFile file(path);
	def.innerGap = 0;
	def.brightBorder = 0;
	if (file.open(QIODevice::ReadOnly | QIODevice::Text))
	{
		QXmlStreamReader reader(&file);
		while (!reader.atEnd())
		{
			QXmlStreamReader::TokenType nType = reader.readNext();

			switch (nType)
			{
			case QXmlStreamReader::StartElement:
			{
				QString strElementName = reader.name().toString();
				//  qDebug()<<strElementName;

				if (strElementName.compare("chip", Qt::CaseInsensitive) == 0)
				{
					QXmlStreamAttributes attributes = reader.attributes();
					//QString name, value;
					if (attributes.hasAttribute("padNumbers")) 
					{
						def.padNumbers = attributes.value("padNumbers").toInt();
					}
					if (attributes.hasAttribute("ballNumbers"))
					{
						def.ballNumbers = attributes.value("ballNumbers").toInt();
					}
					if (attributes.hasAttribute("capNumbers"))
					{
						def.capNumbers = attributes.value("capNumbers").toInt();
					}
				/*	if (attributes.hasAttribute("crossMark"))
					{
						def.crossMark = attributes.value("crossMark").toInt();
					}*/
					if (attributes.hasAttribute("edgeGap"))
					{
						def.edgeGap = attributes.value("edgeGap").toDouble();
					}
					if (attributes.hasAttribute("innerGap"))
					{
						def.innerGap = attributes.value("innerGap").toDouble();
					}
					if (attributes.hasAttribute("brightBorder"))
					{
						def.brightBorder = attributes.value("brightBorder").toInt();
					}
					if (attributes.hasAttribute("capArea"))
					{
						def.capArea = attributes.value("capArea").toInt();
					}
					if (attributes.hasAttribute("multiErrorCode"))
					{
						def.multiErrorCode = attributes.value("multiErrorCode").toInt();
					}
				}
				else if (strElementName.compare("topLight", Qt::CaseInsensitive) == 0)
				{
					QXmlStreamAttributes attributes = reader.attributes();
					if (attributes.hasAttribute("color"))
					{
						def.lightTop.color = attributes.value("color").toString();
					}

				}
				else if (strElementName.compare("midLight", Qt::CaseInsensitive) == 0)
				{
					QXmlStreamAttributes attributes = reader.attributes();
					if (attributes.hasAttribute("color"))
					{
						def.lightMid.color = attributes.value("color").toString();
					}

				}
				else if (strElementName.compare("bottomLight", Qt::CaseInsensitive) == 0)
				{
					QXmlStreamAttributes attributes = reader.attributes();
					if (attributes.hasAttribute("color"))
					{
						def.lightBottom.color = attributes.value("color").toString();
					}

				}
				else if (strElementName.compare("assistLight", Qt::CaseInsensitive) == 0)
				{
					QXmlStreamAttributes attributes = reader.attributes();
					if (attributes.hasAttribute("color"))
					{
						def.lightAssist.color = attributes.value("color").toString();
					}
				}
				else if (strElementName.compare("lightConfigBall", Qt::CaseInsensitive) == 0)
				{
					QXmlStreamAttributes attributes = reader.attributes();
					if (attributes.hasAttribute("brightness1"))
					{
						def.lightConfigBall.brightness1 = attributes.value("brightness1").toInt();
					}
					if (attributes.hasAttribute("brightness2"))
					{
						def.lightConfigBall.brightness2 = attributes.value("brightness2").toInt();
					}
					if (attributes.hasAttribute("brightness3"))
					{
						def.lightConfigBall.brightness3 = attributes.value("brightness3").toInt();
					}
					if (attributes.hasAttribute("brightness4"))
					{
						def.lightConfigBall.brightness4 = attributes.value("brightness4").toInt();
					}
				}
				else if (strElementName.compare("lightConfigCM", Qt::CaseInsensitive) == 0)
				{
					QXmlStreamAttributes attributes = reader.attributes();
					if (attributes.hasAttribute("brightness1"))
					{
						def.lightConfigCM.brightness1 = attributes.value("brightness1").toInt();
					}
					if (attributes.hasAttribute("brightness2"))
					{
						def.lightConfigCM.brightness2 = attributes.value("brightness2").toInt();
					}
					if (attributes.hasAttribute("brightness3"))
					{
						def.lightConfigCM.brightness3 = attributes.value("brightness3").toInt();
					}
					if (attributes.hasAttribute("brightness4"))
					{
						def.lightConfigCM.brightness4 = attributes.value("brightness4").toInt();
					}
				}
				else if (strElementName.compare("lightConfigCap", Qt::CaseInsensitive) == 0)
				{
					QXmlStreamAttributes attributes = reader.attributes();
					if (attributes.hasAttribute("brightness1"))
					{
						def.lightConfigCap.brightness1 = attributes.value("brightness1").toInt();
					}
					if (attributes.hasAttribute("brightness2"))
					{
						def.lightConfigCap.brightness2 = attributes.value("brightness2").toInt();
					}
					if (attributes.hasAttribute("brightness3"))
					{
						def.lightConfigCap.brightness3 = attributes.value("brightness3").toInt();
					}
					if (attributes.hasAttribute("brightness4"))
					{
						def.lightConfigCap.brightness4 = attributes.value("brightness4").toInt();
					}
				}
				else if (strElementName.compare("lightConfigSubstrate", Qt::CaseInsensitive) == 0)
				{
					QXmlStreamAttributes attributes = reader.attributes();
					if (attributes.hasAttribute("brightness1"))
					{
						def.lightConfigSubstrate.brightness1 = attributes.value("brightness1").toInt();
					}
					if (attributes.hasAttribute("brightness2"))
					{
						def.lightConfigSubstrate.brightness2 = attributes.value("brightness2").toInt();
					}
					if (attributes.hasAttribute("brightness3"))
					{
						def.lightConfigSubstrate.brightness3 = attributes.value("brightness3").toInt();
					}
					if (attributes.hasAttribute("brightness4"))
					{
						def.lightConfigSubstrate.brightness4 = attributes.value("brightness4").toInt();
					}
				}
				else if (strElementName.compare("lightConfigPad", Qt::CaseInsensitive) == 0)
				{
					QXmlStreamAttributes attributes = reader.attributes();
					if (attributes.hasAttribute("brightness1"))
					{
						def.lightConfigBall.brightness1 = attributes.value("brightness1").toInt();
					}
					if (attributes.hasAttribute("brightness2"))
					{
						def.lightConfigBall.brightness2 = attributes.value("brightness2").toInt();
					}
					if (attributes.hasAttribute("brightness3"))
					{
						def.lightConfigBall.brightness3 = attributes.value("brightness3").toInt();
					}
					if (attributes.hasAttribute("brightness4"))
					{
						def.lightConfigBall.brightness4 = attributes.value("brightness4").toInt();
					}
				}
				else if (strElementName.compare("action", Qt::CaseInsensitive) == 0)
				{
					QXmlStreamAttributes attributes = reader.attributes();
					ACTION_CONFIG ac;
					if (attributes.hasAttribute("id"))
					{
						ac.id = attributes.value("id").toInt();
					}
					if (attributes.hasAttribute("x"))
					{
						ac.x = attributes.value("x").toInt();
					}
					if (attributes.hasAttribute("y"))
					{
						ac.y = attributes.value("y").toInt();
					}
					if (attributes.hasAttribute("catchBall"))
					{
						ac.catchBall = attributes.value("catchBall").toInt();
					}
	/*				if (attributes.hasAttribute("catchPad"))
					{
						ac.catchPad = attributes.value("catchPad").toInt();
					}*/
					if (attributes.hasAttribute("catchCap"))
					{
						ac.catchCap = attributes.value("catchCap").toInt();
					}
					if (attributes.hasAttribute("catchSubstrate"))
					{
						ac.catchSubstrate = attributes.value("catchSubstrate").toInt();
					}
				/*	if (attributes.hasAttribute("catchCM"))
					{
						ac.catchCM = attributes.value("catchCM").toInt();
					}*/
					def.listAction.append(ac);
				}
				break;
			}

			default:
				break;
			}
		}

		if (reader.hasError())
		{
			qDebug() << QString("err:%1  line:%2  column:%3  pos:%4").arg(reader.errorString()).arg(reader.lineNumber()).arg(reader.columnNumber()).arg(reader.characterOffset());
		}

		file.close();
	}

	if (def.innerGap == 0)
	{
		if (m_type.compare("LS3A4000", Qt::CaseInsensitive) == 0 ||
			m_type.compare("LS3A3000", Qt::CaseInsensitive) == 0
			)
		{
			def.innerGap = 0;
		}
		else if (m_type.compare("Mero(FF3)", Qt::CaseInsensitive) == 0 ||
			m_type.compare("Cambricon_Lion", Qt::CaseInsensitive) == 0 ||
			m_type.compare("ZX2100", Qt::CaseInsensitive) == 0)
		{
			def.innerGap = 5.5;
		}
		else
		{
			def.innerGap = 15.5;
		}
	}
	return def;
}

void StaticFunction::SavePackageInfo(PACKAGE_INFO info)
{
	QString appName = QCoreApplication::applicationName();
	QSettings app1(appName, "Config");
	QString chipType = app1.value("ChipType").toString();

	QString m_type = app1.value("Package").toString();
	QString path = QString("%1/Config/package/%3/%2.cfg").arg(QCoreApplication::applicationDirPath()).arg(m_type).arg(chipType);
	QFile file(path);
	if (!file.open(QFile::WriteOnly | QFile::Text)) {
		qDebug() << "Error: Cannot write file: "
			<< qPrintable(file.errorString());

		return;
	}
	QXmlStreamWriter xmlWriter(&file);
	xmlWriter.setAutoFormatting(true);
	xmlWriter.writeStartDocument();
	xmlWriter.writeStartElement("property");
	xmlWriter.writeStartElement("chip");
	xmlWriter.writeAttribute("capArea", QString::number(info.capArea));
	xmlWriter.writeAttribute("ballNumbers", QString::number(info.ballNumbers));
	xmlWriter.writeAttribute("capNumbers", QString::number(info.capNumbers));
//	xmlWriter.writeAttribute("crossMark", QString::number(info.crossMark));
	xmlWriter.writeAttribute("edgeGap", QString::number(info.edgeGap));
	xmlWriter.writeAttribute("innerGap", QString::number(info.innerGap));

	xmlWriter.writeAttribute("multiErrorCode", QString::number(info.multiErrorCode));
	// xmlWriter.writeAttribute("hdVersion", QString::number(info.hdVersion));

	xmlWriter.writeEndElement();

	xmlWriter.writeStartElement("topLight");
	xmlWriter.writeAttribute("color", info.lightTop.color);
	xmlWriter.writeEndElement();

	xmlWriter.writeStartElement("midLight");
	xmlWriter.writeAttribute("color", info.lightMid.color);
	xmlWriter.writeEndElement();

	xmlWriter.writeStartElement("bottomLight");
	xmlWriter.writeAttribute("color", info.lightBottom.color);
	xmlWriter.writeEndElement();

	xmlWriter.writeStartElement("lightConfigBall");
	xmlWriter.writeAttribute("brightness1", QString::number(info.lightConfigBall.brightness1));
	xmlWriter.writeAttribute("brightness2", QString::number(info.lightConfigBall.brightness2));
	xmlWriter.writeAttribute("brightness3", QString::number(info.lightConfigBall.brightness3));
	xmlWriter.writeAttribute("brightness4", QString::number(info.lightConfigBall.brightness4));
	xmlWriter.writeEndElement();

	xmlWriter.writeStartElement("lightConfigCM");
	xmlWriter.writeAttribute("brightness1", QString::number(info.lightConfigCM.brightness1));
	xmlWriter.writeAttribute("brightness2", QString::number(info.lightConfigCM.brightness2));
	xmlWriter.writeAttribute("brightness3", QString::number(info.lightConfigCM.brightness3));
	xmlWriter.writeAttribute("brightness4", QString::number(info.lightConfigCM.brightness4));
	xmlWriter.writeEndElement();

	xmlWriter.writeStartElement("lightConfigCap");
	xmlWriter.writeAttribute("brightness1", QString::number(info.lightConfigCap.brightness1));
	xmlWriter.writeAttribute("brightness2", QString::number(info.lightConfigCap.brightness2));
	xmlWriter.writeAttribute("brightness3", QString::number(info.lightConfigCap.brightness3));
	xmlWriter.writeAttribute("brightness4", QString::number(info.lightConfigCap.brightness4));
	xmlWriter.writeEndElement();

	xmlWriter.writeStartElement("lightConfigSubstrate");
	xmlWriter.writeAttribute("brightness1", QString::number(info.lightConfigSubstrate.brightness1));
	xmlWriter.writeAttribute("brightness2", QString::number(info.lightConfigSubstrate.brightness2));
	xmlWriter.writeAttribute("brightness3", QString::number(info.lightConfigSubstrate.brightness3));
	xmlWriter.writeAttribute("brightness4", QString::number(info.lightConfigSubstrate.brightness4));
	xmlWriter.writeEndElement();


	xmlWriter.writeEndElement();
	xmlWriter.writeEndDocument();
	file.close();
	if (file.error()) {
		qDebug() << "Error: Cannot write file: "
			<< qPrintable(file.errorString());
		return;
	}
}